const db = require('../db/index')

const bcrypt = require('bcryptjs')
const config = require('../config')





exports.updateUserPwd = function(req, res) {
    console.log(req.user);
    const userinfo = req.body
    const sql = 'update ev_users set password=? where id=?'
    userinfo.password = bcrypt.hashSync(userinfo.newPwd, 10)
    console.log(userinfo);
    db.query(sql, [userinfo.password, req.user.id], function(err, result) {
        if (err) {
            return res.cc(err)
        }
        if (result.affectedRows !== 1)
            return res.cc('update password failed')

        res.cc('update password sucessful!', 0)
    })
}

exports.getUserInfo = function(req, res) {
    console.log(req.user)
    const sql = 'select * from ev_users where id=?'
    db.query(sql, req.user.id, function(err, rows) {
        if (err) {
            res.cc(err)
        }
        if (rows.length !== 1) {
            console.log('id', req.user.id);
            return res.cc('用户不存在！')
        }
        const data = {
            ...rows[0],
            password: '',
        }

        res.send({
            status: 0,
            message: '用户查询成功',
            data,
        })
    })
}

exports.updateUserAvater = function(req, res) {
    console.log(req.body);
    const sql = 'update ev_users set user_pic= ? where id= ?'
    db.query(sql, [req.body.avatar, req.user.id], function(err, result) {
        console.log(result);
        if (err)
            return res.cc(err)
        if (result.affectedRows !== 1)
            return res.cc('update avater failed')

        res.cc('update avater successful', 0)
    })

}

exports.updateUserInfo = function(req, res) {
    console.log(req.body)
    const sql = 'update ev_users set ? where id = ?'

    db.query(sql, [req.body, req.body.id], function(err, result) {
        if (err) return res.cc(err)

        if (result.affectedRows !== 1) {
            return rec.cc('update failed')
        }

        res.cc('更新成功 ！', 0)
    })
}